package com.hyd.manage.mapper;

import java.util.List;
import com.hyd.manage.domain.Medicines;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 药品入库Mapper接口
 * 
 * @author hwb
 * @date 2025-02-20
 */
public interface MedicinesMapper 
{
    /**
     * 查询药品入库
     * 
     * @param id 药品入库主键
     * @return 药品入库
     */
    public Medicines selectMedicinesById(Long id);

    /**
     * 查询药品入库列表
     * 
     * @param medicines 药品入库
     * @return 药品入库集合
     */
    public List<Medicines> selectMedicinesList(Medicines medicines);

    /**
     * 新增药品入库
     * 
     * @param medicines 药品入库
     * @return 结果
     */
    public int insertMedicines(Medicines medicines);

    /**
     * 修改药品入库
     * 
     * @param medicines 药品入库
     * @return 结果
     */
    public int updateMedicines(Medicines medicines);

    /**
     * 删除药品入库
     * 
     * @param id 药品入库主键
     * @return 结果
     */
    public int deleteMedicinesById(Long id);

    /**
     * 批量删除药品入库
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteMedicinesByIds(Long[] ids);

    /**
     * 根据批次号查询药品
     * @param batchNumber
     * @return
     */
    @Select("select * from hyd.tb_medicines where batch_number = #{batchNumber}")
    public Medicines selectMedicinesByBatchNumber(String batchNumber);

    /**
     * 根据药品编码查询药品
     * @param medicineCode
     * @return
     */
    @Select("select * from hyd.tb_medicines where medicine_code = #{medicineCode}")
    public List<Medicines> selectMedicinesListByCode(String medicineCode);

    /**
     * 根据药品编码和批次号更新库存（销售）
     * @param medicineCode
     * @param batchNumber
     * @param quantity
     * @return
     */
    @Update("update hyd.tb_medicines set stock_quantity = stock_quantity - #{quantity} where medicine_code = #{medicineCode} and batch_number = #{batchNumber}")
    public int updateStockQuantity(@Param("medicineCode") String medicineCode,
                                   @Param("batchNumber") String batchNumber,
                                   @Param("quantity") Long quantity);

    /**
     * 根据药品编码和批次号更新库存（退货）
     * @param medicineCode
     * @param batchNumber
     * @param quantity
     * @return
     */
    @Update("update hyd.tb_medicines set stock_quantity = stock_quantity + #{quantity} where medicine_code = #{medicineCode} and batch_number = #{batchNumber}")
    public int updateStockQuantityAdd(@Param("medicineCode") String medicineCode,
                                      @Param("batchNumber") String batchNumber,
                                      @Param("quantity") Long quantity);

    /**
     * 根据药品编码查询库存
     * @param medicineCode
     * @return
     */
    @Select("select * from hyd.tb_medicines where medicine_code = #{medicineCode}")
    public List<Medicines> selectInventoryByCode(String medicineCode);

    /**
     * 根据药品编码和批次号查询药品
     * @param medicineCode
     * @param batchNumber
     * @return
     */
    @Select("select * from hyd.tb_medicines where medicine_code = #{medicineCode} and batch_number = #{batchNumber}")
    public Medicines selectMedicineByCodeAndNumber(@Param("medicineCode") String medicineCode,
                                                   @Param("batchNumber") String batchNumber);
}
